package de.freudenreichdesign.webshop.service;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import de.freudenreichdesign.webshop.model.Article;
import de.freudenreichdesign.webshop.persistence.DatabaseUtil;

@Service("articleService")
public class ArticleService {

	private static ArticleService articleService = null;
	private DatabaseUtil databaseUtil = null;
	
	private ArticleService() {
		databaseUtil = DatabaseUtil.getInstance();
	}
	
	public synchronized static ArticleService getInstance() {
		if (articleService == null)
			articleService = new ArticleService();
		return articleService;
	}
	
	public Article getArticleByID(long articleID) {
		return (Article)databaseUtil.getObject(Article.class.getName(), articleID);
	}
	
	public void createArticle(String title, String height,
			String description, String length, String depth, String weight,
			double price, float tax, Date activeSince,
			String imageSmallPath, String imageLargePath) {
		while (databaseUtil.startSaveTransaction() == false);
		Article article = new Article(title, height, description, length, depth,
				weight, price, tax, activeSince, imageSmallPath, imageLargePath);
		databaseUtil.bufferBeforeSaveCommit(article);
		databaseUtil.saveCommit();
		
	}
	
	public void changeArticleData(long articleID, String title, String height,
			String description, String length, String depth, String weight,
			double price, float tax, Date activeSince,
			String imageSmallPath, String imageLargePath) {
		Article article = getArticleByID(articleID);
		while (databaseUtil.startSaveTransaction() == false);
		article.setActiveSince(activeSince);
		article.setDepth(depth);
		article.setDescription(description);
		article.setHeight(height);
		article.setImageLargePath(imageLargePath);
		article.setImageSmallPath(imageSmallPath);
		article.setLength(length);
		article.setPrice(price);
		article.setTax(tax);
		article.setTitle(title);
		article.setWeight(weight);
		databaseUtil.bufferBeforeSaveCommit(article);
		databaseUtil.saveCommit();
	}
	
	public void deleteArticle(long articleID) {
		databaseUtil.remove(databaseUtil.loadObject(Article.class.getName(), articleID));
	}
	
	public void setArticleInactive(long articleID, Date inactiveDate) {
		Article article = getArticleByID(articleID);
		while (databaseUtil.startSaveTransaction() == false);
		article.setInactiveSince(inactiveDate);
		databaseUtil.bufferBeforeSaveCommit(article);
		databaseUtil.saveCommit();
	}
	
	public List<Object> getListOfAllActiveArticles() {
		//return databaseUtil.executeQuery("from Article as article where article.activeSince >= current_timestamp() and article.inactiveSince <= current_timestamp()");
		return databaseUtil.executeQuery("from Article as article where article.activeSince <= current_timestamp()");
	}
	
	public List<Object> getListOfAllArticles() {
		return databaseUtil.executeQuery("from Article");
	}
}
